<?php
/**
 * @author zheng
 * @date 2018/04/12
 * 微信支付操作
 */
namespace app\index\service;
use think\Cache;
use think\Db;


class WxchatService{
    //商户信息
    protected  $appid;
    protected  $appkey;

    function __construct($appid)
    {
        $this->appid =  $appid;
        //$this->appkey = cache('appkey');
    }

    public function getphone($encryptedData='',$iv = '',$rd_session = ''){

        if (strlen(cache($rd_session)['appkey']) != 24) {
            return(['status'=>0,'msg'=>'4001']);
        }
        $aesKey=base64_decode(cache($rd_session)['appkey']);
        if (strlen($iv) != 24) {
            return (['status'=>0,'msg'=>'4002']);
        }
        $aesIV=base64_decode($iv);

        $aesCipher=base64_decode($encryptedData);

        $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

        $dataObj=json_decode( $result ,true);
        if( $dataObj  == NULL )
        {
            return (['status'=>0,'msg'=>'4003']);
        }

        if( $dataObj->watermark->appid != $this->appid )
        {
            return (['status'=>0,'msg'=>'4004']);
        }
        $data = $result;
        return $data;

    }

}